<?php
namespace User\Controller;
use Think\Controller;
/**
 * 员工自助信息修改控制器 InfoController.class.php
 */
class InfoController extends CommonController{
    /* 属性声明 */
    private $main_conf_file = null;
    /* 方法声明 */
    /**
     * 构造方法
     */
    public function __construct() {
        parent::__construct();
        //检测用户合法性
        if(!$this -> uuid = session('uuid')){
            $this -> error('获取员工标识失败!');
        }
        $this -> corpid = session('corpid');
        $this -> main_conf_file = "./Org/Data/UserData/main_conf/{$this->corpid}_main.conf";
        //获取成员数据
        $this -> redis -> select(0);
        $this -> info = json_decode($this -> redis -> hget('user_list',$this->uuid),true);
        $this -> sub_list = $this -> info['information'];
    }
    
    /**
     * 修改个人信息
     */
    public function edit(){
        $data = I('post.');
        //检测是否是员工基本信息
        if($data['table_name']=='BO_CASC_HR_YGRS'){
            $this -> updateBaseInfo($data);
        }else{
            $this -> updateSubItem($data,$data['table_name']);
        }
    }
    
    /**
     * 修改员工基本信息
     * @param array $data post上传数据
     * @return void
     */
    private function updateBaseInfo($data){
        $temp_data = null;
        //记录索引位置
        $offset = null;
        foreach($this -> sub_list as $k => $v){
            if($v['name']==='BO_CASC_HR_YGRS'){
                $offset = $k;
                $temp_data = $v['sublist'];
                break;
            }
        }
        //修改数据
        foreach($temp_data as $k => $v){
            if(array_key_exists($v['name'], $data)){
                //情况1:value oldvalue 两者全无值状态
                if(($v['value']=='') && ($v['oldvalue']=='')){
                    $temp_data[$k]['oldvalue'] = '无';
                    $temp_data[$k]['value'] = $data[$v['name']];
                }elseif(($v['value']!='') && ($v['oldvalue']=='')){
                    //情况2:有值状态 oldvalue为空
                    $temp_data[$k]['oldvalue'] = $v['value'];
                    $temp_data[$k]['value'] = $data[$v['name']];
                }elseif(($v['value']!='')&&($v['oldvalue']!='')){
                    //情况3:有值状态 oldvalue不为空
                    //处理方式:old不变
                    $temp_data[$k]['value'] = $data[$v['name']];
                }elseif(($v['value']=='') && ($v['oldvalue']!='')){
                    //情况4:无值状态 oldvalue不为空
                    $temp_data[$k]['value'] = $data[$v['name']];
                }
            }
        }
        //回填数据
        $info = $this -> info;
        $info['information'][$offset]['sublist'] = $temp_data;
        $info = json_encode($info);
        //回写数据
        $this -> redis -> hset('user_list',$this->uuid,$info);
        $this -> success('修改成功!');
    }
    
    /**
     * 修改员工子集信息
     */
    private function updateSubItem($data,$table_name){
        $temp_data = null;
        $offset = null;
        foreach($this->sub_list as $k => $v){
            if($v['name']==$table_name){
                $temp_data = $v;
                $offset = $k;
                break;
            }
        }
        //无值状态
        if($data['has_value']==0){
            //初始化数据
            //生成新值
            $new_data = $this -> createNewData($data,$temp_data['sublist']);
            $info = $this -> info;
            $info['information'][$offset]['subvalue'][0]['info'] = $new_data;
            $info['information'][$offset]['subvalue'][0]['awsid'] = '';
            $info['information'][$offset]['status']['insert'][] = 0;
            //回写数据
            $info = json_encode($info);
            $this -> redis -> hset('user_list',$this->uuid,$info);
            $this -> success('编辑成功!');
        }else{
        //有值状态
            //获取表名
            $table_name = $data['table_name'];
            //获取数据坐标
            $_offset_id = $data['_offset_id'];
            //检测是否为新建数据
            $status = $temp_data['status'];
            if(!in_array($_offset_id,$status['insert'])){
                //索引为已有数据
                $status['update'][] = $_offset_id;
                //去重处理
                $status['update'] = array_unique($status['update']);
            }
            //获取指定值集合数据
            $temp_data = $temp_data['subvalue'][$_offset_id]['info'];
            //修改数据
            foreach($temp_data as $k => $v){
                if(array_key_exists($v['name'], $data)){
                        //情况1:value oldvalue 两者全无值状态
                    if(($v['value']=='') && ($v['oldvalue']=='')){
                        $temp_data[$k]['oldvalue'] = '无';
                        $temp_data[$k]['value'] = $data[$v['name']];
                    }elseif(($v['value']!='') && ($v['oldvalue']=='')){
                        //情况2:有值状态 oldvalue为空
                        $temp_data[$k]['oldvalue'] = $v['value'];
                        $temp_data[$k]['value'] = $data[$v['name']];
                    }elseif(($v['value']!='')&&($v['oldvalue']!='')){
                        //情况3:有值状态 oldvalue不为空
                        //处理方式:old不变
                        $temp_data[$k]['value'] = $data[$v['name']];
                    }elseif(($v['value']=='') && ($v['oldvalue']!='')){
                        //情况4:无值状态 oldvalue不为空
                        $temp_data[$k]['value'] = $data[$v['name']];
                    }
                }
            }
            //回填数据
            $info = $this -> info;
            $info['information'][$offset]['subvalue'][$_offset_id]['info'] = $temp_data;
            $info['information'][$offset]['status'] = $status;
            $info = json_encode($info);
            //回写数据
            $this -> redis -> hset('user_list',$this->uuid,$info);
            $this -> success('修改成功!');
        }
    }
    
    /**
     * 生成新数据
     * @param array $data post数据
     * @param array $item 子集数据结构
     * @return array 返回生成新值子集数组
     */
    private function createNewData($data,$item){
        foreach($item as $k => $v){
            if(array_key_exists($v['name'],$data)){
                $item[$k]['value'] = $data[$v['name']];
                if($data[$v['name']]!=''){
                    $item[$k]['oldvalue'] = '无';
                }else{
                    $item[$k]['oldvalue'] = '';
                }
            }
        }
        return $item;
    }
    
    /**
     * 添加新数据
     */
    public function addInfo(){
        $data = I('get.');
        if(!$menu = $data['menu']){
            $this -> error('非法参数传递!');
        }
        $file = "./Org/Data/Original_name_submodel.json";
        $data = json_decode(file_get_contents($file),true);
        $all_menu_list = array_column($data,'name');
        if(!$index = array_search($menu,$all_menu_list)){
            $this -> error('未知子集信息!');
        }
        $sublist = $data[$index];
        $sublist = $this -> cut_caption($sublist);
        $list = $sublist['sublist'];
        //检测是否存在主配置文件
        if(file_exists($this->main_conf_file)){
            $conf = unserialize(file_get_contents($this->main_conf_file));
            foreach($conf as $k => $v){
               if($v['name'] == $menu){
                   $conf = $v;
                   break;
               }
            }
            //0.过滤标准
            $filterlist = $conf['sublist'];
            //1.过滤list
            $name_list = array_column($list,'name');
            $sublist_buf = [];
            //过滤子集显示项 + 排序操作
            foreach($filterlist as $k => $v){
                $offset = array_search($v,$name_list);
                $sublist_buf[] = $list[$offset];
            }
            $sublist['sublist'] = $sublist_buf;
        }
        //初始化前台js字典文件
        $dic = '/Org/Data/Dic_js/'.$this->corpid.'_dic.js';
        $pro = '/Org/Data/Dic_js/'.$this->corpid.'_pro.js';
        
        $this -> assign('sublist',json_encode($sublist));
        $this -> assign([
            'dic' => $dic,
            'pro' => $pro,
        ]);
        $this -> display('add');
    }
    
    /**
     * 截取caption长度为8个汉字长度
     */
    private function cut_caption($dat){
        $list = $dat['sublist'];
        foreach($list as $k => $v){
            if(mb_strlen($v['caption'],'utf-8')>7){
                $list[$k]['caption'] = mb_substr($v['caption'],0,7,'utf-8').'..';
            }
        }
        $dat['sublist'] = $list;
        return $dat;
    }
    
    /**
     * 执行添加新数据
     */
    public function addok(){
        $data = I('post.');
        $temp_data = null;
        $table_name = $data['table_name'];
        $offset = null;
        foreach($this->sub_list as $k => $v){
            if($v['name']==$table_name){
                $temp_data = $v;
                $offset = $k;
                break;
            }
        }
        $res = $this -> createNewData($data,$temp_data['sublist']);
        $info = $this -> info;
        $index = count($info['information'][$offset]['subvalue']);
        //生成新数据下标
        $new_index = $index+1;
        $info['information'][$offset]['subvalue'][$new_index]['info'] = $res;
        $info['information'][$offset]['subvalue'][$new_index]['awsid'] = '';
        $info['information'][$offset]['status']['insert'][] = $new_index;
        //数据回写
        $info = json_encode($info);
        //回写数据
        $this -> redis -> hset('user_list',$this->uuid,$info);
        $this -> success('添加成功!','/User/List/index/corpid/'.session('corpid').'/menu/'.$table_name);
    }
    
    /**
     * 删除子集数据
     */
    public function delInfo(){
        //收集所需数据
        $data = I('get.');
        $tablename = $data['tablename'];
        $_offset_id = $data['id'];
        $info = $this -> info;
        $sub_list = $info['information'];
        $temp_list = null;
        $index = null;
        foreach($sub_list as $k => $v){
            if($v['name']==$tablename){
                $temp_list = $v;
                $index = $k;
                break;
            }
        }
        //分析更改状态
        $status = $temp_list['status'];
        $offset = array_search($_offset_id,$status['insert']);
        //1.新增数据情况 直接删除该新增状态
        if($offset!==false){
            unset($status['insert'][$offset]);
            $status['insert'] = array_values($status['insert']);
        }else if($offset = array_search($_offset_id,$status['update'])){
        //检测该删除数据$offset id 是否在update状态中 有则移除
            unset($status['update'][$offset]);
            $status['delete'][] = $temp_list['subvalue'][$_offset_id]['awsid'];
        }else{
            $status['delete'][] = $temp_list['subvalue'][$_offset_id]['awsid'];
        }
        //3.移除数据
        unset($temp_list['subvalue'][$_offset_id]);
        $temp_list['status'] = $status;
        //回填数据
        $info = $this -> info;
        $info['information'][$index] = $temp_list;
        $info = json_encode($info);
        //回写数据
        $this -> redis -> hset('user_list',$this->uuid,$info);
        $this -> success('删除成功!','/User/List/index/menu/'.$tablename.'/corpid/'.session('corpid'));
    }
    
    /**
     * 生成字典数据
     */
    public function dic(){
        $this -> user_dic_file = './Org/Data/UserData/dic_conf/'.$this->corpid.'_dic_conf.conf';
        //1.检测是否存在用户自定义字典配置
        if(file_exists($this->user_dic_file)){
            $data = json_decode(file_get_contents($this->user_dic_file),true);
            return [1,file_get_contents($this->user_dic_file)];
        }else{
        //2.返回默认字典数据
           $data = json_decode(file_get_contents('./Org/Data/init_dic.json'),true)['CASC.HR.DICT.ALL'];
           return [0,json_encode($data)];
        }
    }
    
    /**
     * 获取省份字典
     */
    public function pro_dic(){
        $this -> user_pro_file = './Org/Data/UserData/province/'.$this->corpid.'_pro_conf.conf';
        //检测是否存在省份字典配置文件
        if(file_exists($this -> user_pro_file)){
            return file_get_contents($this -> user_pro_file);
        }
        //返回默认省份字典文件
        return file_get_contents('./Org/Data/Original_Province.json');
    }
}